package your_car_status

import org.springframework.dao.DataIntegrityViolationException

class CustomerController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index() {
        redirect(action: "list", params: params)
    }

    def list(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        [customerInstanceList: Customer.list(params), customerInstanceTotal: Customer.count()]
    }

    def listVehicles(){
        def veh=Customer.get(session.data.id).vehicle
        [Customer:session.data, vehicleInstanceList: veh]
    }
    
    def searchVehicle (String licensePlate){
        def vehicleInstance
        def veh=Customer.get(session.data.id).vehicle
        licensePlate = licensePlate.toUpperCase()
        for (Vehicle vehicle: veh){
            if(vehicle.getLicensePlate().equals(licensePlate)){
            vehicleInstance = vehicle
            }
        }   
        [vehicleInstanceList: vehicleInstance]
    }
    
    def create() {
        [customerInstance: new Customer(params)]
    }

    def save() {
        def customerInstance = new Customer(params)
        if (!customerInstance.save(flush: true)) {
            render(view: "create", model: [customerInstance: customerInstance])
            return
        }

        flash.message = message(code: 'default.created.message', args: [message(code: 'customer.label', default: 'Customer'), customerInstance.id])
        redirect(action: "show", id: customerInstance.id)
    }

    def show(Long id) {
        def customerInstance = Customer.get(id)
        if (!customerInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'customer.label', default: 'Customer'), id])
            redirect(action: "list")
            return
        }

        [customerInstance: customerInstance]
    }
    
    def showCustomer(Long id) {
        def customerInstance = Customer.get(id)
        if (!customerInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'customer.label', default: 'Customer'), id])
            redirect(action: "list")
            return
        }

        [customerInstance: customerInstance]
    }
    
    def showVehicle(Long id) {
        def vehicleInstance = Vehicle.get(id)
        if (!vehicleInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'vehicle.label', default: 'Vehicle'), id])
            redirect(action: "list")
            return
        }

        [vehicleInstance: vehicleInstance]
    }
    
    

    def edit(Long id) {
        def customerInstance = Customer.get(id)
        if (!customerInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'customer.label', default: 'Customer'), id])
            redirect(action: "list")
            return
        }

        [customerInstance: customerInstance, Taller: session.data]
    }
    
    def editCustomer(Long id) {
        def customerInstance = Customer.get(id)
        if (!customerInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'customer.label', default: 'Customer'), id])
            redirect(action: "list")
            return
        }

        [customerInstance: customerInstance, Taller: session.data]
    }

    def update(Long id, Long version) {
        def customerInstance = Customer.get(id)
        if (!customerInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'customer.label', default: 'Customer'), id])
            redirect(action: "list")
            return
        }

        if (version != null) {
            if (customerInstance.version > version) {
                customerInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                    [message(code: 'customer.label', default: 'Customer')] as Object[],
                          "Another user has updated this Customer while you were editing")
                render(view: "editCustomer", model: [customerInstance: customerInstance])
                return
            }
        }

        customerInstance.properties = params

        if (!customerInstance.save(flush: true)) {
            render(view: "edit", model: [customerInstance: customerInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'customer.label', default: 'Customer'), customerInstance.id])
        redirect(action: "edit", id: customerInstance.id)
    }

    def delete(Long id) {
        def customerInstance = Customer.get(id)
        if (!customerInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'customer.label', default: 'Customer'), id])
            redirect(action: "list")
            return
        }

        try {
            customerInstance.delete(flush: true)
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'customer.label', default: 'Customer'), id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
            flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'customer.label', default: 'Customer'), id])
            redirect(action: "show", id: id)
        }
    }
    
    def showInventory(Long id) {
        def inventoryInstance = Inventory.get(id)
        if (!inventoryInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'inventory.label', default: 'Inventory'), id])
            redirect(action: "list")
            return
        }

        [inventoryInstance: inventoryInstance]
    }
    
    def showDiagnostic(Long id) {
        def diagnosticInstance = Diagnostic.get(id)
        if (!diagnosticInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'diagnostic.label', default: 'Diagnostic'), id])
            redirect(action: "list")
            return
        }

        [diagnosticInstance: diagnosticInstance]
    }
    
    def listDiagnostic(Long id) {
        def veh=Vehicle.get(id).diagnostic;
        [diagnosticInstanceList: veh, diagnosticInstanceTotal: Diagnostic.count(),carId:id]
    }

    def listInventory(Long id) {
        def veh=Vehicle.get(id).inventory;
        [inventoryInstanceList: veh, inventoryInstanceTotal: Inventory.count(),carId:id]
    }

    def listService(Long id) {
        def veh=Vehicle.get(id).services;
        [servicesInstanceList: veh, servicesInstanceTotal: Services.count(), carId:id]
    }
    
}
